package cn.itcast.listener;

import cn.itcast.utils.MailUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import javax.mail.MessagingException;
import java.util.Map;

public class EmailListener implements MessageListener {


    @Override  //只要指定监听的队列中有数据 该方法就执行
    public void onMessage(Message message) {
        try {
            //发邮件
            byte[] body = message.getBody();
            Map<String,String> map = JSON.parseObject(body,Map.class);
            String email = map.get("email");
            String emailMsg = map.get("emailMsg");
            MailUtils.sendMail(email,emailMsg);
            System.out.println("邮件发送成功~");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败~");
        }
    }
}
